CC = sdcc
CFLAGS= -mz80 --code-loc 42513 --no-std-crt0 --data-loc 0 --reserve-regs-iy --max-allocs-per-node 100000

all: main.8xp testvar.8xv

main.8xp: main.bin
	binpac8x -O CSYSCALL main.bin main.8xp

main.bin: main.ixh
	objcopy -Iihex -Obinary main.ihx main.bin

main.ixh: main.rel syscalls.rel crt0.rel
	$(CC) $(CFLAGS) main.rel syscalls.rel crt0.rel

main.rel: main.c syscalls.h
	$(CC) $(CFLAGS) -c main.c

syscalls.rel: syscalls.c syscalls.h
	$(CC) $(CFLAGS) -c syscalls.c

crt0.rel: crt0.s
	sdasz80 -p -g -o crt0.rel crt0.s

testvar.8xv: testvar.dat.rgb565.rle
	to8xv -a -o testvar.8xv testvar.dat.rgb565.rle TESTVAR

reimu.ppm:
	wget https://tomli.blog/ftp/IT/reimu.ppm -O reimu.ppm

testvar.dat: reimu.ppm
	sed '1d; 2d; 3d; 4d' reimu.ppm > testvar.dat

testvar.dat.rgb565: testvar.dat
	$(MAKE) -C tools
	./tools/1-rgb565 testvar.dat

testvar.dat.rgb565.rle: testvar.dat.rgb565
	./tools/2-rle testvar.dat.rgb565

clean:
	$(MAKE) clean -C tools
	rm -rf *.bin *.8xp *.rel *.ihx *.lk *.lst *.sym *.map *.noi main.asm syscalls.asm syscalls.ihx testvar.8xv testvar.dat.*
